package framework.controller;

import com.alibaba.fastjson.JSONObject;
import framework.bo.SysRole;
import framework.common.ResponseVo;
import framework.util._BeanUtil;
import framework.util.http._HttpRequestUtil;
import framework.util.layui.TreeVo;
import framework.util.layui._LayuiUtil;
import framework.util.queryFilter.service.impl.QueryFilter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import frameworkExt.util.controller.BaseControllerExt;
import framework.service.SysRoleService;
;import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/framework/SysRole.do")
public class SysRoleController extends BaseControllerExt {

    @Resource
    private SysRoleService service;

    @ResponseBody
    @RequestMapping(params = "method=list")
    public ResponseVo list(ModelAndView model, HttpServletRequest request, HttpServletResponse response){
        QueryFilter filter = new QueryFilter(request, true);
        List<SysRole> list = service.getAll(filter);
        return sendSuccess(list, filter.getPageBean().getTotals());
    }

    @ResponseBody
    @RequestMapping(params = "method=add")
    public ResponseVo add(HttpServletRequest request, SysRole bo){
        setOperatorToBo(bo);
        service.add(bo);
        return sendSuccess();
    }

    @ResponseBody
    @RequestMapping(params = "method=delete")
    public ResponseVo delete(ModelAndView model, HttpServletRequest request, HttpServletResponse response){
        String lid = request.getParameter("lid");
        if (StringUtils.isBlank(lid)) {
            return sendFail("参数错误");
        }
        service.removeById(Integer.valueOf(lid));
        return sendSuccess();
    }

    @RequestMapping(params = "method=toEditOrDetail")
    public ModelAndView toEditOrDetail(ModelAndView model, HttpServletRequest request, HttpServletResponse response) {
        String lid = request.getParameter("lid");
        String pageUrl = request.getParameter("pageUrl");
        if(StringUtils.isAnyBlank(lid, pageUrl)){
            return null;
        }

        SysRole bo = service.get(Integer.valueOf(lid));
        model.addObject("bo", bo);
        model.setViewName(pageUrl);
        return model;
    }

    @ResponseBody
    @RequestMapping(params = "method=update")
    public ResponseVo update(ModelAndView model, HttpServletRequest request, HttpServletResponse response){
        String lid = request.getParameter("lid");
        if(StringUtils.isBlank(lid)){
            return sendFail("参数错误");
        }

        try {
            SysRole oldBo = service.get(Integer.valueOf(lid));
            _BeanUtil.copyPropertiesFromRequest(oldBo, request);
            service.update(oldBo);
            return sendSuccess();
        } catch (IllegalAccessException | InvocationTargetException e) {
            log.error("", e);
            return sendError(e.getMessage());
        }
    }

}
